package cn.iocoder.yudao.module.train.service.teacher;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.module.train.dal.dataobject.WxUser.WxUserDO;
import cn.iocoder.yudao.module.train.dal.dataobject.connectDTO.TeacherUserDTO;
import cn.iocoder.yudao.module.train.dal.mysql.WxUser.WxUserMapper;
import cn.iocoder.yudao.module.train.utils.DateUtils;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.train.controller.admin.teacher.vo.*;
import cn.iocoder.yudao.module.train.dal.dataobject.teacher.TeacherDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.train.dal.mysql.teacher.TeacherMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.train.enums.ErrorCodeConstants.*;

/**
 * 教师用户 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class TeacherServiceImpl implements TeacherService {

    @Resource
    private TeacherMapper teacherMapper;

    @Resource
    private WxUserMapper wxUserMapper;

    @Override
    public Long createTeacher(TeacherSaveReqVO createReqVO) {
        // 插入
        TeacherDO teacher = BeanUtils.toBean(createReqVO, TeacherDO.class);
        teacher.setTeacherAge(DateUtils.calculateAge(teacher.getTeacherBirthdate()));
        teacherMapper.insert(teacher);

        // 返回
        return teacher.getId();
    }

    @Override
    public void updateTeacher(TeacherSaveReqVO updateReqVO) {
        // 校验存在
        validateTeacherExists(updateReqVO.getId());
        if (wxUserMapper.selectById(updateReqVO.getUserId()) == null) {
            throw exception(WX_USER_NOT_EXISTS);
        }
        // 更新
        TeacherDO updateObj = BeanUtils.toBean(updateReqVO, TeacherDO.class);
        updateObj.setTeacherAge(DateUtils.calculateAge(updateObj.getTeacherBirthdate()));
        WxUserDO wxUpdateObj = BeanUtils.toBean(updateReqVO, WxUserDO.class);
        wxUpdateObj.setId(updateObj.getUserId());
        wxUserMapper.updateById(wxUpdateObj);
        teacherMapper.updateById(updateObj);
    }

    @Override
    public void deleteTeacher(Long id) {
        // 校验存在
        validateTeacherExists(id);
        // 删除
        teacherMapper.deleteById(id);
    }

    @Override
        public void deleteTeacherListByIds(List<Long> ids) {
        // 删除
        teacherMapper.deleteByIds(ids);
        }


    private void validateTeacherExists(Long id) {
        if (teacherMapper.selectById(id) == null) {
            throw exception(TEACHER_NOT_EXISTS);
        }
    }

    @Override
    public TeacherUserDTO getTeacher(Long id) {
        return teacherMapper.selectTeacherUser(id);
    }

    @Override
    public PageResult<TeacherUserDTO> getTeacherPage(TeacherPageReqVO pageReqVO) {
        return teacherMapper.selectPage(pageReqVO);
    }

    @Override
    public List<TeacherUserDTO> getTeacherList(TeacherPageReqVO pageReqVO) {
        return teacherMapper.selectList(pageReqVO);
    }
}
